package com.info.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "SetCharacterEncoding",urlPatterns="/*")
public class SetCharacterEncoding implements Filter {
    //该方法必须加上，否则项目启动失败
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //添加过滤器的方法
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");

        System.out.println("执行过滤器方法....");

        //我们必须在方法最后加上这么一句，我们的过滤器才会被执行到，否则不会执行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
